<?php

namespace app\Api\event;

use think\Controller;
use app\common\Token;
use think\Exception;
use think\Lang;
use think\Collection;
use EasyWeChat\Foundation\Application;
use EasyWeChat\MiniProgram\Encryption\Encryptor;
use EasyWeChat\MiniProgram\Notice\Notice;
use EasyWeChat\MiniProgram\AccessToken;

class User extends Controller
{
    public function getLogin()
    {
        return $this->getToken();
    }
    
    public function getToken()
    {
        if (!Token::checkToken($this->request->only(['pass', 'mobile', 'sign'], 'header'))) {
            throw new Exception(Lang::get("Sign checksum failed"), 1);            
        }

    	$mobile = $this->request->header('mobile');
    	$pass   = $this->request->header('pass');
    	
        try {
            $user_model = \think\Loader::model('Users')->getUserInfo(['mobile' => $mobile]);
        } catch (\Exception $e) {
            throw new Exception(Lang::get("user does not exist"), 1);
        }
        
        if (md5($pass) !== $user_model->password) {
            throw new Exception(Lang::get("Password error"), 1);
        }

        return ['code' => 200, 'data' => ['token' => $this->apps->setToken($user_model->id), 'user_info' => $user_model]];
    }

    public function getWxsession()
    {
        return $this->apps->wxsession();
    }

    public function getUserSign()
    {
        return $this->request->user->sign;
    }

    public function getUserinfo()
    {
        $user_other_infos = [
            'user_info'     => $this->request->user->hidden(['updated_at','created_at','login_session_id','password']),
            'user_capital'  => $this->request->user->getUserCapitals(),
            'user_notices'  => $this->request->user->getNoticeList()->hidden(['sender','send_time']),
            'user_caravans' => $this->getUserCaravan()->hidden(['updated_at','created_at','deleted_at']),
            'user_wishs'    => $this->request->user->getWishs()->hidden(['updated_at']),
            'user_orders'   => $this->request->user->getOrders()->hidden(['updated_at'])
        ];

        return Collection::make($user_other_infos);
    }

    public function getUserCaravan()
    {
        return $this->request->user->getUserCaravans();
    }

    public function getCapitals()
    {
        return \app\api\controller\Index::$api_user->getUserCapitals();
    }

    public function getNotice()
    {
        return $this->apps->notice();
    }

    public function getNoticeList()
    {
        return $this->request->user->getNoticeList();
    }

    public function getReadNotice()
    {
        return $this->request->user->readNotice($this->request->param('id'));
    }

    public function postNoticesAllIRead()
    {
        return $this->request->user->setAllNoticesIsRead();
    }

    /**
     * 设置房车状态为分享
     * @return boolean [description]
     */
    public function postSetShare()
    {
        $caravan_id = $this->request->param('caravan_id');
        $start_date = $this->request->param('start_date');
        $end_date   = $this->request->param('end_date');

        return $this->request->user->getCaravanInfo($caravan_id)->setCaravanShare($start_date, $end_date);
    }

    /**
     * 设置房车状态为托管
     * @return boolean [description]
     */
    public function postSetTrust()
    {
        $caravan_id = $this->request->param('caravan_id');
        $start_date = $this->request->param('start_date');
        $end_date   = $this->request->param('end_date');

        return $this->request->user->getCaravanInfo($caravan_id)->setCaravanTrusteeship($start_date, $end_date);
    }

    /**
     * 设置房车状态为自用
     * @return boolean [description]
     */
    public function postSetHome()
    {
        $caravan_id = $this->request->param('caravan_id');
        $start_date = $this->request->param('start_date');
        $end_date   = $this->request->param('end_date');

        return $this->request->user->getCaravanInfo($caravan_id)->setCaravanHome($start_date, $end_date);
    }
}
